
cc.Class({
    extends: cc.Component,

    properties: {
        sprite:{
            default:null,
            type:cc.Sprite
        },

        //put as bottom pokers
        dragModel:{
            default:null,
            type:cc.Prefab
        },

        //put on top so that player can darg to get one poker
        dragableModel:{
            default:null,
            type:cc.Prefab
        },

        arrowNode:{
            default:null,
            type:cc.Node,
        },

        borderNode:{
            default:null,
            type:cc.Node
        },

        noticePokerNode:{
            default:null,
            type:cc.Node
        },

        noticePokerSprite:{
            default:null,
            type:cc.Sprite
        },

        dragableParent:{
            default:null,
            type:cc.Node
        }
    },


    onLoad () {
        if(!cc.gg){
            cc.gg = {}
        }
        cc.gg.disCardMgr = this

        this.rightX = 160
        this.interval = 40
        this.stackShowCount = 5
        this.arrowOriPos = cc.v2(100,3)

        this.stackPokerNodes = []
        this.discardPokers = []     //仅存储当前显示的五张牌面值
        this.borderNode.active = false

        this.node.on("ui_dispokerstack_start_blink",this.startBlink.bind(this),this)
        this.node.on("ui_dispokerstack_stop_blink",this.stopBlink.bind(this),this)

        cc.find("Canvas").on("flow_player_turn",this._onDisStackPlayerTurnStart.bind(this),this)

        // cc.find("Canvas").on("ui_show_discard_notice",this.showDiscardNotice.bind(this),this)
    },

    // onDestroy(){
    //     cc.find("Canvas").on("flow_player_turn",this._onDisStackPlayerTurnStart.bind(this),this)
    // },

    initDispokerDeck:function (deckWorldY,handWorldY,mixNum) {
        this.deckWorldY = deckWorldY
        this.handWorldY = handWorldY
        this.mixNum = mixNum
    },

    showDiscardNotice:function (show, pokerValue) {
        if(show){
            if(!this.noticePokerNode.active) this.noticePokerNode.active = true
            cc.gg.resUtil.setPokerFrameSimple(this.noticePokerSprite.getComponent(cc.Sprite),pokerValue)
        }else {
            if(this.noticePokerNode.active) this.noticePokerNode.active = false
        }
    },

    //return the dragging poker to discard stack when turn changed
    _onDisStackPlayerTurnStart:function () {
        if(!cc.gg.handPokerMgr.draggingNode) return

        let script = cc.gg.handPokerMgr.draggingNode.getComponent("PokerModelTs")
        if(script.fromDisPokerStack){
            script.setDisPokerAttr(true,false)
            cc.gg.emitEvent("player_cancel_grab_from_discards",script.node)
        }
    },

    startBlink:function () {

        this.borderNode.active = true
        this.borderNode.stopAllActions()
        cc.log("开始闪烁==========",this.borderNode.opacity,this.borderNode.active)

        var fadeIn = cc.fadeIn(0.8)
        var fadeOut = cc.fadeOut(0.8)
        let seq = cc.sequence(fadeIn,fadeOut)
        let repeat = cc.repeatForever(seq)

        this.borderNode.opacity = 0
        this.borderNode.runAction(repeat)
    },

    stopBlink:function () {
        // this.borderNode.stopAllActions()
        this.borderNode.active = false
        // this.borderNode.opacity = 0
    },

    start () {

    },

    update (dt) {
        // cc.log(this.borderNode.active,"  ",this.borderNode.opacity)
    },

    _addOnePokerModel:function(zIndex){
        let pokerNode = cc.instantiate(this.dragableModel)
        pokerNode.zIndex = zIndex
        pokerNode.position = cc.Vec2.ZERO

        this.dragableParent.addChild(pokerNode)
        this.stackPokerNodes.push(pokerNode)

        let pokerModelTs =pokerNode.getComponent("PokerModelTs")
        pokerModelTs.setDisPokerAttr(true,false)
    },

    clearDiscardsStack:function () {
        cc.logfl("clear:discard stack clear...")
        this.discardPokers = []
        for(let i=0;i<this.stackPokerNodes.length;i++){
            this.stackPokerNodes[i].active = false
        }
    },

    updateDiscardStack:function (pokerObjs,deckWorldY,handWorldY,mixNum) {

        // cc.log("更新弃牌库：",pokerObjs)

        this.discardPokers = pokerObjs
        let lackCount = this.stackShowCount-this.stackPokerNodes.length
        for(let i=0;i<lackCount;i++){
            // cc.log("            ui:为弃牌堆增加一个节点")
            this._addOnePokerModel(this.stackPokerNodes.length-1)
        }


        deckWorldY = deckWorldY || cc.gg.tpgMgr.pokerStackNode.parent.convertToWorldSpaceAR(cc.gg.tpgMgr.pokerStackNode.position).y
        handWorldY = handWorldY || cc.gg.tpgMgr.handPokerParent.parent.convertToWorldSpaceAR(cc.gg.tpgMgr.handPokerParent.position).y
        for(let i=0;i<this.stackPokerNodes.length;i++){
            let pokerObj = this.discardPokers[this.discardPokers.length - this.stackShowCount + i]
            let pokerNode = this.stackPokerNodes[i]
            if(!pokerObj){
                pokerNode.poker = null
                pokerNode.active = false
                continue
            }

            // let topPoker = cc.gg.disCardMgr.discardPokers[cc.gg.disCardMgr.discardPokers.length - 1]
            pokerNode.getComponent("PokerModelTs").initPoker(pokerObj,handWorldY,deckWorldY,mixNum)
            pokerNode.poker = pokerObj
            pokerNode.active = true
            // pokerNode.scaleX = 4
            // pokerNode.scaleY = 4
            // pokerNode.position = cc.Vec2.ZERO
            pokerNode.getComponent("PokerModelTs")._disPokerDragable = false
        }

        let lastPokerNode = this.getLastPoker()
        if(lastPokerNode)
            lastPokerNode.getComponent("PokerModelTs")._disPokerDragable = true

        // cc.log("弃牌堆顶牌：",lastPokerNode)

        this.hideTopPokers()
        // cc.log("当前所有弃牌：",this.discardPokers)
    },

    showTopPokers:function (deckWorldY,handWorldY,mixNum) {
        this._showingDisStack = true
        this.arrowNode.scaleX = -1
        this._playExpandAnim(deckWorldY,handWorldY,mixNum)
    },

    onButtonArrowClicked:function (event) {
        if(!this.stackPokerNodes)
            return

        if(!this._showingDisStack){
            this.showTopPokers(this.deckWorldY,this.handWorldY,this.mixNum)
        }else {
            this.hideTopPokers()
        }
    },

    _playExpandAnim:function (deckWorldY,handWorldY,mixNum) {
        if(!this.stackPokerNodes){
            cc.error("Logic Error!")
            return
        }

        let maxX = 0
        let disIdx = 0
        deckWorldY = deckWorldY || cc.gg.tpgMgr.pokerStackNode.parent.convertToWorldSpaceAR(cc.gg.tpgMgr.pokerStackNode.position).y
        handWorldY = handWorldY || cc.gg.tpgMgr.handPokerParent.parent.convertToWorldSpaceAR(cc.gg.tpgMgr.handPokerParent.position).y
        for(let i=0;i<this.stackPokerNodes.length;i++){
            let pokerObj = this.discardPokers[this.discardPokers.length - this.stackShowCount + i]
            if(!pokerObj)
                continue

            let newNode = this.stackPokerNodes[i]
            let x = this.interval * disIdx++

            newNode.getComponent("PokerModelTs").initPoker(pokerObj,handWorldY,handWorldY,mixNum)

            newNode.active = true
            newNode.runAction(cc.moveTo(0.1,x,0))
            maxX = x
        }

        this.arrowNode.runAction(cc.moveTo(0.1,maxX + 100,0))

        this.scheduleOnce(function () {
            this.expanding  = false
        }.bind(this), 0.1)

        this.expanding  = true
    },

    hideTopPokers:function () {
        this.arrowNode.position = this.arrowOriPos

        this._showingDisStack = false
        this.arrowNode.scaleX = 1
        let children = this.dragableParent.children
        for(let i=0;i<children.length;i++){
            let node = children[i]
            if(node === this.sprite.node)
                continue

            if(node === this.arrowNode)
                continue

            if(node === this.borderNode)
                continue

            node.stopAllActions()
            let moveTo = cc.moveTo(0.1,cc.Vec2.ZERO)
            node.runAction(moveTo)
        }
    },

    removePokerNodeFromDisStack:function (pokerNode) {
        // cc.gg.disCardMgr.discardPokers.remove(pokerNode.poker)
        cc.gg.baseUtil.arrayRemove(cc.gg.disCardMgr.discardPokers,pokerNode.poker)
        // this.stackPokerNodes.remove(pokerNode)
        cc.gg.baseUtil.arrayRemove(this.stackPokerNodes,pokerNode)
    },

    returnPokerNode:function (pokerNode) {
        cc.gg.disCardMgr.discardPokers.push(pokerNode.poker)
        this.stackPokerNodes.push(pokerNode)
    },

    getLastPoker:function () {
        let lastPoker = this.discardPokers[this.discardPokers.length - 1]
        for(let i=0;i<this.stackPokerNodes.length;i++){
            if(this.stackPokerNodes[i].poker === lastPoker) return this.stackPokerNodes[i]
        }
        return null
    }
});
